package com.mcxx.modules.common.service.impl;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.common.service.StatisticService;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.statistics.dto.*;
import com.mcxx.modules.statistics.param.StatisticParam;
import com.mcxx.modules.statistics.service.read.StatisticReadService;
import com.mcxx.modules.system.entity.AreaSalvationEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
@TaTransactional
public class StatisticPovertySummaryService extends StatisticService {
    @Autowired
    private StatisticReadService statisticReadService;

    @Autowired
    private AreaReadService areaReadService;

    @Override
    public List<? extends StatisticDTO> getStatistic(StatisticParam param, UserAccountVo accountVo, String standType) {
        List<TaAreaPo> taAreaPos = new ArrayList<TaAreaPo>();
        List<Integer> levels = new ArrayList<Integer>();
        String name = super.commonStatisticStart(param, accountVo, taAreaPos, levels);
        if(taAreaPos.isEmpty()){
            throw new BizException(FailureCode.ERR_100002);
        }

        TaAreaPo curTaAreaPo = taAreaPos.get(0);

        String validCode = areaUtil.getValidCode(curTaAreaPo.getAreaCode(), curTaAreaPo.getAreaLevel());

        List<TaAreaPo> childArea = areaReadService.getChildrenByLevel(validCode, levels);
        List<TaAreaPo> validCounty = childArea.stream().filter(e -> !e.getAreaCode().endsWith("99900")
                && !e.getAreaName().startsWith("测试") && !e.getAreaName().endsWith("省本级")).collect(Collectors.toList());


        Integer level = curTaAreaPo.getAreaLevel();
        if (curTaAreaPo.getAreaLevel() < Constant.AreaLevel.COMMUNITY)
            level += 1;

        String formatDate = param.getMonth();
        formatDate = formatDate.substring(0, 4) + "-" + formatDate.substring(4) + "-01";

        List<PovertyApprovalDTO> results = statisticReadService.getPovertySummary(validCode, level, 0, null,
                                                                                formatDate, standType);


        List<PovertyApprovalDTO> tempResults = results
                .stream().filter(e ->
                        !((e.getAllfocusNursingNum() + e.getAllfocusSelfcareNum() + e.getHalffocusNursingNum() + e.getAllNursingNum() + e.getHalfNursingNum()+ e.getAllSelfcareNum())==0)
        ).collect(Collectors.toList());

        List<PovertyApprovalDTO> basics = null;
        if(standType.equals(Constant.BusinessStandType.CITY_TEKUN)){
            name += "城市特困审批汇总表";
            basics = statisticReadService.getCityPovertySummaryBasic(validCode, level, 0, null, formatDate);
        }
        else{
            name += "农村特困审批汇总报表";
            basics = statisticReadService.getCountryPovertySummaryBasic(validCode, level, 0, null, formatDate);
        }
        List<PovertyApprovalDTO> salarys = statisticReadService.getPovertySummarySalary(validCode, level,
                0, null, formatDate, standType);

        for(PovertyApprovalDTO dto : tempResults){
            dto.setTitle(name);
            Optional<PovertyApprovalDTO> basicObj = basics.stream().filter(e -> e.getAreaCode().equals(dto.getAreaCode())).findFirst();
            if (basicObj.isPresent()) {
                PovertyApprovalDTO obj = basicObj.get();
                dto.setBasiclifefund(obj.getBasiclifefund());
                dto.setAllNursing(obj.getAllNursing());
                dto.setHalfNursing(obj.getHalfNursing());
                dto.setAllSelfcare(obj.getAllSelfcare());
            }

            Optional<PovertyApprovalDTO> salaryObj = salarys.stream().filter(e -> e.getAreaCode().equals(dto.getAreaCode())).findFirst();
            if (salaryObj.isPresent()) {
                PovertyApprovalDTO obj1 = salaryObj.get();
                dto.setBasic(obj1.getBasic());
                dto.setCare(obj1.getCare());
                dto.setZidingyi(obj1.getZidingyi());
            }
        }

        List<PovertyApprovalDTO> lastResults = new ArrayList<>();
        for (TaAreaPo taArea : validCounty) {
            String taAreaValidCode = areaUtil.getValidCode(taArea.getAreaCode(), taArea.getAreaLevel());
            List<PovertyApprovalDTO> list = tempResults.stream().filter(temp -> temp.getAreaCode().startsWith(taAreaValidCode)).collect(Collectors.toList());

            PovertyApprovalDTO dto = new PovertyApprovalDTO();
            dto.setAreaCode(taArea.getAreaCode());
            dto.setArea(taArea.getAreaName());
            dto.setTitle(name);

            PovertyApprovalDTO.sumForField(list,dto);

            lastResults.add(dto);
        }
        super.commonStatisticEnd(name, null, taAreaPos, levels, lastResults);

        return lastResults;
    }

    @Override
    public void fillSalvation(StatisticDTO dto, List<String> standards, List<AreaSalvationEntity> salivations) {

    }
}
